package com.owc.common.custom.validator;

import com.owc.common.tool.ValidationUtils;
import com.owc.common.custom.annotation.IsPhone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author xiaom
 * @Date 2019/10/22 11:06
 * @Version 1.0.0
 * @Description < 电话号码格式校验器 >
 **/
public class IsPhoneValidator implements ConstraintValidator<IsPhone, String> {
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {

        //Disable default hint message
        constraintValidatorContext.disableDefaultConstraintViolation();
        if(s == null){
            constraintValidatorContext
                    .buildConstraintViolationWithTemplate("Phone number must not be null.").addConstraintViolation();
            return false;
        }

        if(!ValidationUtils.isMobile(s)){
            //Use default hint message
            constraintValidatorContext
                    .buildConstraintViolationWithTemplate(" Illegal phone number format ").addConstraintViolation();
            return false;
        }

        return true;
    }
}
